package com.sign2024.sign2024project.capitalconstruction.service.impl;

import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjHistoricalMajor;
import com.sign2024.sign2024core.entry.capitalconstruction.JjMajorCollect;
import com.sign2024.sign2024core.entry.capitalconstruction.JjUpdateProject;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjHistoricalMajorMapper;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjMajorCollectMapper;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjUpdateProjectMapper;
import com.sign2024.sign2024project.capitalconstruction.service.JjUpdateDataService;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/9/13 17:25
 */
@Service
public class JjUpdateDataServiceImpl implements JjUpdateDataService {

    @Resource
    private JjUpdateProjectMapper jjUpdateProjectMapper;

    @Resource
    private JjHistoricalMajorMapper jjHistoricalMajorMapper;

    @Resource
    private JjMajorCollectMapper jjMajorCollectMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;

    @Override
    public void addExport(List<JjUpdateProject> projectList, String fileName) {
        if (projectList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }

        // 添加数据
        for (JjUpdateProject wz : projectList) {
            wz.setFileName(fileName);
            jjUpdateProjectMapper.insert(wz);
        }
    }

    @Override
    public void saveJJUpdate(Integer taskState) {
        List<JjUpdateProject> projectList = jjUpdateProjectMapper.selectListAll();
        if (projectList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请导入抽取方案");
        }

        // 获取历史版本号
        int code = 0;
        Integer versionCode = jjHistoricalMajorMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }

        // 获取 表示中 所有数据
        List<JjMajorCollect> relationList = jjMajorCollectMapper.selectListAll();
        // 如果 获取的数据 是 空的 说明 当前数据时 第一版本
        if (!relationList.isEmpty()) {
            for (JjMajorCollect ss : relationList) {
                JjHistoricalMajor h = new JjHistoricalMajor();
                h.setVersionCode(code);
                h.setEpcTwo(ss.getEpcTwo());
                h.setSignName(ss.getSignName());
                h.setSignType(ss.getSignType());
                // 商务 1 - 5
                h.setBiesMajorOne(ss.getBiesMajorOne());
                h.setBiesMajorTwo(ss.getBiesMajorTwo());
                h.setBiesMajorThree(ss.getSkillMajorThree());
                h.setBiesMajorFour(ss.getBiesMajorFour());
                h.setBiesMajorFive(ss.getBiesMajorFive());
                // 技术 1 - 7
                h.setSkillMajorOne(ss.getSkillMajorOne());
                h.setSkillMajorTwo(ss.getSkillMajorTwo());
                h.setSkillMajorThree(ss.getSkillMajorThree());
                h.setSkillMajorFour(ss.getSkillMajorFour());
                h.setSkillMajorFive(ss.getSkillMajorFive());
                h.setSkillMajorSix(ss.getSkillMajorSix());
                h.setSkillMajorEight(ss.getSkillMajorEight());
                jjHistoricalMajorMapper.insert(h);
            }
        }

        // 更新 数据
        for (JjUpdateProject wz : projectList) {
            // 根据 签报名称 查询 数据
//            WzSpecialtySignRelation relation = wzSpecialtySignRelationMapper.selectWzRelation(wz.getSectionName());

            JjMajorCollect major = jjMajorCollectMapper.selectMajorByBName(wz.getEcpName());

            if (StringUtils.isNotNull(major)) {
                // 如果数据存在 修改
                JjMajorCollect h = new JjMajorCollect();
                h.setId(major.getId());
                h.setEpcTwo(wz.getEcpName());
                h.setSignName(wz.getSignName());

                if (StringUtils.isNotNull(wz.getBossMajor())) {
                    // 商务 1 - 5
                    h.setBiesMajorOne(wz.getBossMajor());
                    // 1 - 2
                    if (StringUtils.isNotNull(major.getBiesMajorOne())) {
                        h.setBiesMajorTwo(major.getBiesMajorOne());
                    }

                    // 2 -3
                    if (StringUtils.isNotNull(major.getBiesMajorTwo())) {
                        h.setBiesMajorThree(major.getBiesMajorTwo());
                    }

                    // 3 - 4
                    if (StringUtils.isNotNull(major.getBiesMajorThree())) {
                        h.setBiesMajorFour(major.getBiesMajorThree());
                    }

                    // 4 -5
                    if (StringUtils.isNotNull(major.getBiesMajorFour())) {
                        h.setBiesMajorFive(major.getBiesMajorFour());
                    }
                }

                if (StringUtils.isNotNull(wz.getSkillMajor())) {
                    // 技术 1 -7
                    h.setSkillMajorOne(wz.getSkillMajor());
                    // 1 - 2
                    if (StringUtils.isNotNull(major.getSkillMajorOne())) {
                        h.setSkillMajorTwo(major.getSkillMajorOne());
                    }

                    // 2 -3
                    if (StringUtils.isNotNull(major.getSkillMajorTwo())) {
                        h.setSkillMajorThree(major.getSkillMajorTwo());
                    }

                    // 3 - 4
                    if (StringUtils.isNotNull(major.getSkillMajorThree())) {
                        h.setSkillMajorFour(major.getSkillMajorThree());
                    }

                    // 4 -5
                    if (StringUtils.isNotNull(major.getSkillMajorFour())) {
                        h.setSkillMajorFive(major.getSkillMajorFour());
                    }

                    // 5 - 6
                    if (StringUtils.isNotNull(major.getSkillMajorFive())) {
                        h.setSkillMajorSix(major.getSkillMajorFive());
                    }

                    // 6 - 7
                    if (StringUtils.isNotNull(major.getSkillMajorSix())) {
                        h.setSkillMajorEight(major.getSkillMajorSix());
                    }
                }
                jjMajorCollectMapper.updateById(h);
            } else {
                // 如果数据存在 修改
                JjMajorCollect h = new JjMajorCollect();
                h.setEpcTwo(wz.getEcpName());
                h.setSignName(wz.getSignName());
                // 商务 1 - 5
                h.setBiesMajorOne(wz.getBossMajor());
                // 技术 1 -7
                h.setSkillMajorOne(wz.getSkillMajor());
                jjMajorCollectMapper.insert(h);
            }

            // 添加 回退记录  taskState 任务标识
            RollbackRecord rollback = new RollbackRecord();
            // 文件名
            String projectName = projectList.get(0).getFileName();

            rollback.setFileName(projectName);
            // 任务标识
            rollback.setTaskState(taskState);
            // 数据 版本号
            rollback.setVersionCode(code);
            rollbackRecordMapper.insert(rollback);

            // 数据 更新完成 清空数据
            jjUpdateProjectMapper.cleanJjUpdateProject();
        }
    }
}
